package cn.stylefeng.guns.modular.yingbao.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.yingbao.entity.UserName;
import cn.stylefeng.guns.modular.yingbao.model.params.UserNameParam;
import cn.stylefeng.guns.modular.yingbao.service.UserNameService;
import cn.stylefeng.guns.sys.core.util.UeditorUtil;
import cn.stylefeng.guns.sys.modular.system.model.UeditorFileResult;
import cn.stylefeng.guns.sys.modular.system.model.UploadResult;
import cn.stylefeng.guns.sys.modular.system.service.FileInfoService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import oracle.jdbc.proxy.annotation.Post;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;


/**
 * 实名认证表控制器
 *
 * @author zs
 * @Date 2019-12-11 13:51:25
 */
@Api(tags = "实名认证模块")
@Controller
@RequestMapping("/userName")
@CrossOrigin
public class UserNameController extends BaseController {

    private String PREFIX = "/userName";

    @Autowired
    private UserNameService userNameService;

    /**
     * 跳转到主页面
     *
     * @author zs
     * @Date 2019-12-11
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/userName.html";
    }

    /**
     * 新增页面
     *
     * @author zs
     * @Date 2019-12-11
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/userName_add.html";
    }

    /**
     * 编辑页面
     *
     * @author zs
     * @Date 2019-12-11
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/userName_edit.html";
    }

    /**
     * 新增接口
     *
     * @author zs
     * @Date 2019-12-11
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(UserNameParam userNameParam) {
        this.userNameService.add(userNameParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author zs
     * @Date 2019-12-11
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(UserNameParam userNameParam) {
        this.userNameService.update(userNameParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author zs
     * @Date 2019-12-11
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(UserNameParam userNameParam) {
        this.userNameService.delete(userNameParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author zs
     * @Date 2019-12-11
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(UserNameParam userNameParam) {
        UserName detail = this.userNameService.getById(userNameParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * layui上传组件 通用文件上传接口
     *
     * @author fengshuonan
     * @Date 2019-2-23 10:48:29
     */


    @ApiOperation("上传图片")
    @RequestMapping(method = RequestMethod.POST, path = "/upload")
    @ResponseBody
    public ResponseData layuiUpload(@RequestPart("file") MultipartFile file) {

        UeditorFileResult uploadResult = UeditorUtil.uploadFile(file,UeditorUtil.FileType.IMG);
        String fileSavePath = uploadResult.getUrl();

        return ResponseData.success(0, "上传成功", fileSavePath);
    }

    @ApiOperation("富文本上传图片")
    @RequestMapping(method = RequestMethod.POST, path = "/images1")
    @ResponseBody
    public ResponseData layuiImages1(@RequestPart("file") MultipartFile file) {

        UeditorFileResult uploadResult = UeditorUtil.uploadFile(file,UeditorUtil.FileType.IMG);
        if(uploadResult.getState().equals("SUCCESS")){
            Map<String,String> map = new HashMap<>();
            map.put("src","http://23.252.161.47"+uploadResult.getUrl());
            map.put("title",map.get("src"));
            return ResponseData.success(0,"上传成功",map);
        }
        return ResponseData.error(500,"上传失败");
        /*String fileSavePath = uploadResult.getUrl();
        if()

        return ResponseData.success(0, "上传成功", fileSavePath);*/
    }
    /**
     * 查询列表
     *
     * @author zs
     * @Date 2019-12-11
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(UserNameParam userNameParam) {
        return this.userNameService.findPageBySpec(userNameParam);
    }


    @PostMapping("addUserName")
    @ResponseBody
    @ApiOperation("添加实名认证")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userid",value = "用户id"),
            @ApiImplicitParam(name = "name",value = "姓名"),
            @ApiImplicitParam(name = "identity",value = "身份证号"),
            @ApiImplicitParam(name = "justpic",value = "正面照"),
            @ApiImplicitParam(name = "againstpic",value = "反面照")
    })
    public ResponseData addUserName(UserName userName){
        return this.userNameService.addUserName(userName);
    }

    @PostMapping("queryByUserId")
    @ApiOperation("是否实名认证")
    @ApiImplicitParam(name = "userid",value = "用户id")
    @ResponseBody
    public ResponseData queryByUserId(String userid){
        return this.userNameService.queryByUserId(userid);
    }

    @PostMapping("queryByPhone")
    @ApiOperation("根据手机号判断是否需要需要上传身份证")
    @ResponseBody
    @ApiImplicitParam(name = "phone", value = "手机号")
    public ResponseData queryByPhone(String phone){
        return this.userNameService.queryByPhone(phone);
    }

    /**
     * 后台接口
     */
    @PostMapping("upStatus")
    @ResponseBody
    @ApiOperation("修改实名认证状态")
    public ResponseData upStatus(String id,String status){
        return this.userNameService.upStatus(id,status);
    }

    @PostMapping("delById")
    @ResponseBody
    @ApiOperation("删除实名认证状态")
    public ResponseData delById(String id){
        return this.userNameService.delById(id);
    }
}


